package base.sort;

import java.util.Arrays;

class BaseSort {
    public static void main(String[] args) {
        int[] arr = {10, -2, 32, 43, -45, 0, -2};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    // 简单选择
    public static int[] selectAlgorithm(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int index = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[index] > arr[j]) index = j;
            }
            int tmp = arr[index];
            arr[index] = arr[i];
            arr[i] = tmp;
        }

        return arr;
    }

    // 冒泡
    public static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            boolean flag=false;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j]>arr[j+1]){
                    int tmp = arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tmp;
                    flag=true;
                }
            }
            if (!flag) break;
        }
    }

    // 快速排序
    public static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int middle = divide(arr, low, high);
            quickSort(arr, low, middle - 1);
            quickSort(arr, middle + 1, high);
        }
    }

    public static int divide(int[] arr, int low, int high) {
        int tmp = arr[low];

        while (low < high) {
            while (low < high && arr[high] >= tmp) high--;
            arr[low] = arr[high];

            while (low < high && arr[low] <= tmp) low++;
            arr[high] = arr[low];
        }

        arr[low] = tmp;
        return low;
    }
}
